﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ComponentModel;
using GalaSoft.MvvmLight.Threading;

namespace Cleverscape.UTorrentClient.WebClient
{
    public class FeedItem : INotifyPropertyChanged
    {

        private string _Name;

        public string Name
        {
            get { return _Name; }
            set
            {

                if (_Name != value && PropertyChanged != null)
                {
                    _Name = value;
                    NotifyPropertyChanged("Name");
                }
                _Name = value;
            }
        }

        private string _NameFull;

        public string NameFull
        {
            get { return _NameFull; }
            set
            {

                if (_NameFull != value && PropertyChanged != null)
                {
                    _NameFull = value;
                    NotifyPropertyChanged("NameFull");
                }
                _NameFull = value;
            }
        }

        private string _Url;

        public string Url
        {
            get { return _Url; }
            set
            {

                if (_Url != value && PropertyChanged != null)
                {
                    _Url = value;
                    NotifyPropertyChanged("Url");
                }
                _Url = value;
            }
        }

        private int _Quality;

        public int Quality
        {
            get { return _Quality; }
            set
            {

                if (_Quality != value && PropertyChanged != null)
                {
                    _Quality = value;
                    NotifyPropertyChanged("Quality");
                }
                _Quality = value;
            }
        }

        private int _Codec;

        public int Codec
        {
            get { return _Codec; }
            set
            {

                if (_Codec != value && PropertyChanged != null)
                {
                    _Codec = value;
                    NotifyPropertyChanged("Codec");
                }
                _Codec = value;
            }
        }

        private int _TimeStamp;

        public DateTime TimeStamp
        {
            get { return Utilities.ConvertFromUnixTimestamp( _TimeStamp ); }
            set
            {

                if (_TimeStamp != Utilities.ConvertToUnixTimestamp( value ) && PropertyChanged != null)
                {
                    _TimeStamp = Utilities.ConvertToUnixTimestamp(value);
                    NotifyPropertyChanged("TimeStamp");
                }
                
            }
        }

        private int _Season;

        public int Season
        {
            get { return _Season; }
            set
            {

                if (_Season != value && PropertyChanged != null)
                {
                    _Season = value;
                    NotifyPropertyChanged("Season");
                }
                _Season = value;
            }
        }

        private int _Episode;

        public int Episode
        {
            get { return _Episode; }
            set
            {

                if (_Episode != value && PropertyChanged != null)
                {
                    _Episode = value;
                    NotifyPropertyChanged("Episode");
                }
                _Episode = value;
            }
        }

        private int _EpisodeTo;

        public int EpisodeTo
        {
            get { return _EpisodeTo; }
            set
            {

                if (_EpisodeTo != value && PropertyChanged != null)
                {
                    _EpisodeTo = value;
                    NotifyPropertyChanged("EpisodeTo");
                }
                _EpisodeTo = value;
            }
        }

        private int _FeedID;

        public int FeedID
        {
            get { return _FeedID; }
            set
            {

                if (_FeedID != value && PropertyChanged != null)
                {
                    _FeedID = value;
                    NotifyPropertyChanged("FeedID");
                }
                _FeedID = value;
            }
        }

        private bool _Repack;

        public bool Repack
        {
            get { return _Repack; }
            set
            {

                if (_Repack != value && PropertyChanged != null)
                {
                    _Repack = value;
                    NotifyPropertyChanged("Repack");
                }
                _Repack = value;
            }
        }

        

        private bool _InHistory;

        public bool InHistory
        {
            get { return _InHistory; }
            set
            {
                if (_InHistory != value && PropertyChanged != null)
                {
                    _InHistory = value;
                    NotifyPropertyChanged("InHistory");
                }
                _InHistory = value;
            }
        }






        public FeedItem(object[] InputValues)
        {
            UpdateValues(InputValues);
        }

        internal void UpdateValues(object[] InputValues)
        {
            this.Name = (string)InputValues[0];
            this.NameFull = (string)InputValues[1];
            this.Url = (string)InputValues[2];
            this.Quality = (int)InputValues[3];
            this.Codec = (int)InputValues[4];
            _TimeStamp = (int)InputValues[5];
            this.Season = (int)InputValues[6];
            this.Episode = (int)InputValues[7];
            this.EpisodeTo = (int)InputValues[8];
            this.FeedID = (int)InputValues[9];
            this.Repack = (bool)InputValues[10];
            this.InHistory = (bool)InputValues[11];
        }



        #region INotifyPropertyChanged Members

        public event PropertyChangedEventHandler PropertyChanged;

        private void NotifyPropertyChanged(string PropertyName)
        {

            if (DispatcherHelper.UIDispatcher != null)
            {
                DispatcherHelper.UIDispatcher.BeginInvoke(() =>
                {
                    if (PropertyChanged != null)
                    {
                        PropertyChanged(this, new PropertyChangedEventArgs(PropertyName));
                    }
                });
            }
            else
            {
                if (PropertyChanged != null)
                {
                    PropertyChanged(this, new PropertyChangedEventArgs(PropertyName));
                }
            }

        }

        #endregion
    }
}
